package org.example.mr.wc;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author Heshan On 2021/5/23
 */
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 测试下如果改为局部变量会发生什么？
        Text text = new Text();
        IntWritable count = new IntWritable(1);
        String[] words = value.toString().split(" ");
        for(String word :words) {
            text.set(word);
            context.write(text, count);
        }
    }
}
